using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.Diagnostics;

namespace MyClever.Lib
{ 
    public class MissionPluginPackageColumn : DataColumn
    {
        public enum ExtendedPropertiesKey
        {
            Description,
            Validation,
            ColumnReference,
            CellReference,
            UseDataStream // is this currently important?
        }

        #region Properties
        public string Description
        {
            get
            {
                if (true == this.ExtendedProperties.ContainsKey(ExtendedPropertiesKey.Description))
                {
                    return this.ExtendedProperties[ExtendedPropertiesKey.Description].ToString();
                }
                return "";
            }
        }

        public bool HasValidation
        {
            get
            {
                return this.ExtendedProperties.ContainsKey(ExtendedPropertiesKey.Validation);
            }
        }

        public string Validation
        {
            get
            {
                Debug.Assert(true == this.HasValidation);
                return this.ExtendedProperties[ExtendedPropertiesKey.Validation].ToString();
            }
        }

        public bool HasDefaultValue
        {
            get
            {
                return !String.IsNullOrEmpty(this.DefaultValue.ToString());
            }
        }

        public bool HasColumnReference
        {
            get
            {
                return this.ExtendedProperties.ContainsKey(ExtendedPropertiesKey.ColumnReference);
            }
        }

        public PackageColumnReference ColumnReference
        {
            set
            {
                this.ExtendedProperties[ExtendedPropertiesKey.ColumnReference] = value;
            }
            get
            {
                Debug.Assert(true == this.HasColumnReference);
                return (PackageColumnReference)this.ExtendedProperties[ExtendedPropertiesKey.ColumnReference];
            }
        }

        public bool HasCellReference
        {
            get
            {
                return this.ExtendedProperties.ContainsKey(ExtendedPropertiesKey.CellReference);
            }
        }

        public PackageCellReference CellReference
        {
            set
            {
                this.ExtendedProperties[ExtendedPropertiesKey.CellReference] = value;
            }
            get
            {
                Debug.Assert(true == this.HasCellReference);
                return (PackageCellReference)this.ExtendedProperties[ExtendedPropertiesKey.CellReference];
            }
        }
        #endregion
        
        // todo: use copy-constructor here
        /// <summary>
        /// Use this constructor for "copy" or "clone" an instance of the <see cref="MissionPluginPackage"/> class only!.
        /// </summary>
        public MissionPluginPackageColumn()
            : base()
        {
        }
        
        public MissionPluginPackageColumn(string columnName, Type dataType) : 
            base(columnName, dataType)
        {
        }

        public MissionPluginPackageColumn(PackageColumnDefinition columnDefinition) :
            base(columnDefinition.name, System.Type.GetType(columnDefinition.valueType))
        {
            this.DefaultValue = columnDefinition.defaultValue;

            if (null != columnDefinition.extendedProperties)
            {
                foreach (KeyValuePair<MissionPluginPackageColumn.ExtendedPropertiesKey, object> extendedPropertyOn in columnDefinition.extendedProperties)
                {
                    this.ExtendedProperties.Add(extendedPropertyOn.Key, extendedPropertyOn.Value);
                }
            }
        }

        public PackageColumnDefinition GetDefinition()
        {
            PackageColumnDefinition columnDefinition = new PackageColumnDefinition(this.ColumnName, this.DataType, this.DefaultValue);

            foreach (DictionaryEntry entryOn in this.ExtendedProperties)
            {
                columnDefinition.extendedProperties.Add((ExtendedPropertiesKey)entryOn.Key, entryOn.Value);
            }
            return columnDefinition;
        }
        
        public bool IsCompatible(MissionPluginPackageColumn column)
        {
            if (null == column)
            {
                return false;
            }
            
            // same value?
            // while a string can convert in every other datatype theoretically,
            // we ignore every comparison here
            bool sameDataType = true;
            if (false == (typeof(string) == this.DataType || typeof(string) == column.DataType))
            {
                sameDataType = (this.DataType == column.DataType);
            }

            return sameDataType;
        }

        public MissionPluginPackageColumn Clone()
        {
            MissionPluginPackageColumn clone = new MissionPluginPackageColumn(this.ColumnName, this.DataType);

            clone.DefaultValue = this.DefaultValue;

            // deep copy of all extendedProperties
            clone.ExtendedProperties.Clear();
            foreach (DictionaryEntry entryOn in this.ExtendedProperties)
            {
                clone.ExtendedProperties.Add(entryOn.Key, entryOn.Value);
            }
            return clone;
        }
    }
}
